package util
{
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import flash.ui.MouseCursorData;
	import flash.utils.getTimer;
	
	/**
	 * Used for global game settings
	 */
	public class Settings
	{
		/** Meter to pixel ratio */
		public static const METER:Number = 20;
		
		/** Used for debugging. Draws bounding boxes for all field units and stuff. */
		public static var draw_bounding_boxes:Boolean;
		/** */
		private static var stage:Stage;
		/** */
		private static var last_time:int;
		/** */
		private static var avg_fps:Number;
		
		private static var _time_warp:Number;
		
		/** */
		public static function init(s:Stage):void
		{
			stage = s;
			last_time = getTimer();
			avg_fps = getTargetFrameRate();
			stage.addEventListener(Event.EXIT_FRAME, tick, false, 0, true);
			
			_time_warp = 1.0;
			
			// mouse
			var cursor_data:MouseCursorData = new MouseCursorData();
			var data:Vector.<BitmapData> = new Vector.<BitmapData>(1, true);
			data[0] = new BitmapData(16, 16, true, 0x00000000);
			
			var shape:Shape = new Shape();
			data[0].draw(shape);
			cursor_data.data = data;
			cursor_data.hotSpot = new Point(0, 0);
			cursor_data.frameRate = 0;
			Mouse.registerCursor("cTest", cursor_data);
		
			//Mouse.cursor = "cTest";
		}
		
		/**
		 * Update the framerate counter.
		 */
		static private function tick(e:Event):void
		{
			var current_time:int = getTimer();
			avg_fps = (0.9 * avg_fps) + (0.1 * int(1000.0 / (current_time - last_time)));
			last_time = current_time;
		}
		
		/**
		 * @return	Current actual number of frames per second
		 */
		public static function getFrameRate():int
		{
			return avg_fps;
		}
		
		/**
		 * @return	Intedned number of frames per second
		 */
		public static function getTargetFrameRate():int
		{
			return stage.frameRate;
		}
		
		/**
		 * @return	Number of seconds per update
		 */
		public static function getTimeStep():Number
		{
			return _time_warp / getFrameRate();
		}
		
		/**
		 * Set the intended frame rate
		 * @param	n
		 */
		public static function setTargetFrameRate(n:int):void
		{
			stage.frameRate = n;
		}
		
		static public function get time_warp():Number 
		{
			return _time_warp;
		}
		
		static public function set time_warp(value:Number):void 
		{
			_time_warp = MyMath.max2(value, 0);
		}
	
	}

}